//
//  HttpRequestManager.m
//  news_ios_app
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HttpRequestManager.h"

@implementation HttpRequestManager

+(AFHTTPRequestOperationManager *)sharedDefault
{
    static AFHTTPRequestOperationManager *operationManager;
    static dispatch_once_t request_dispatch_once;
    dispatch_once(&request_dispatch_once, ^{
        operationManager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL new]];
        //请求表单时用plain
         NSMutableSet *acceptTypes = operationManager.responseSerializer.acceptableContentTypes.mutableCopy;
        [acceptTypes addObject:@"text/plain"];
        [acceptTypes addObject:@"text/html"];
        operationManager.responseSerializer.acceptableContentTypes = acceptTypes.copy;
    });
    return operationManager;
}

+(void)HttpRequestWithUrl:(NSString *)url
                parameter:(NSDictionary *)parameter
                  success:(RequestSuccess)success
                  failure:(RequestFailure)failure
{
    AFHTTPRequestOperationManager *operationmanager = [self sharedDefault];
    [parameter setValue:appKey forKey:@"sign"];
    [operationmanager POST:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            if (error) {
                NSLog(@"%@\n%@",error,operation);
            }
        }
    }];
}

+(void)HttpRequestForNewsTypeWithParameter:(NSDictionary *)parameter
                             success:(RequestSuccess)success
                             failure:(RequestFailure)failure
{
    [HttpRequestManager HttpRequestWithUrl:findType parameter:parameter success:success failure:failure];
}

+(void)HttpRequestForArticleByTypeIdWithParameter:(NSDictionary *)parameter
                                    success:(RequestSuccess)success
                                    failure:(RequestFailure)failure
{
    [HttpRequestManager HttpRequestWithUrl:findArticleByTypeId parameter:parameter success:success failure:failure];
}

+(void)HttpRequestForArticleDetailByIdWithParameter:(NSDictionary *)parameter
                                      success:(RequestSuccess)success
                                      failure:(RequestFailure)failure
{
    [HttpRequestManager HttpRequestWithUrl:findArticleDetailById parameter:parameter success:success failure:failure];
}

@end
